package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TMessageBoard;
import com.ruoyi.system.service.ITMessageBoardService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 留言板Controller
 * 
 * @author ruoyi
 * @date 2024-03-05
 */
@Controller
@RequestMapping("/system/board")
public class TMessageBoardController extends BaseController
{
    private String prefix = "system/board";

    @Autowired
    private ITMessageBoardService tMessageBoardService;

    @RequiresPermissions("system:board:view")
    @GetMapping()
    public String board()
    {
        return prefix + "/board";
    }


    @GetMapping("/messageBoard")
    public String messageBoard()
    {
        return prefix + "/messageBoard";
    }


    /**
     * 查询留言板列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TMessageBoard tMessageBoard)
    {
        startPage();
        List<TMessageBoard> list = tMessageBoardService.selectTMessageBoardList(tMessageBoard);
        return getDataTable(list);
    }

    /**
     * 导出留言板列表
     */
    @Log(title = "留言板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TMessageBoard tMessageBoard)
    {
        List<TMessageBoard> list = tMessageBoardService.selectTMessageBoardList(tMessageBoard);
        ExcelUtil<TMessageBoard> util = new ExcelUtil<TMessageBoard>(TMessageBoard.class);
        return util.exportExcel(list, "留言板数据");
    }

    /**
     * 新增留言板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存留言板
     */
    @Log(title = "留言板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody TMessageBoard tMessageBoard)
    {
        SysUser sysUser = getSysUser();
        tMessageBoard.setUserId(sysUser.getUserId());
        tMessageBoard.setUserName(sysUser.getUserName());
        return toAjax(tMessageBoardService.insertTMessageBoard(tMessageBoard));
    }

    /**
     * 修改留言板
     */
    @RequiresPermissions("system:board:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TMessageBoard tMessageBoard = tMessageBoardService.selectTMessageBoardById(id);
        mmap.put("tMessageBoard", tMessageBoard);
        return prefix + "/edit";
    }

    /**
     * 修改保存留言板
     */
    @RequiresPermissions("system:board:edit")
    @Log(title = "留言板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TMessageBoard tMessageBoard)
    {
        return toAjax(tMessageBoardService.updateTMessageBoard(tMessageBoard));
    }

    /**
     * 删除留言板
     */
    @RequiresPermissions("system:board:remove")
    @Log(title = "留言板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tMessageBoardService.deleteTMessageBoardByIds(ids));
    }
}
